﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Library;
using System.ComponentModel;
using OnlineVideos.Data;
using OnlineVideos;
using OnlineVideos.Common;
using System.Threading;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using OnlineVideos.UI;


namespace OnlineMovies.UserControls
{
    public partial class FavoriteAudio : UserControl
    {
        List<ShowLinkInfo> showFavAudio = null;
        public FavoriteAudio()
        {
            InitializeComponent();
            showFavAudio = new List<ShowLinkInfo>();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                BackgroundHelper bwHelper = new BackgroundHelper();
                bwHelper.AddBackgroundTask(
                                                   (object s, DoWorkEventArgs a) =>
                                                   {
                                                       a.Result = FavoritesManager.GetFavoriteAudioLinks();
                                                   },
                                                   (object s, RunWorkerCompletedEventArgs a) =>
                                                   {


                                                       showFavAudio = (List<ShowLinkInfo>)a.Result;
                                                       if (showFavAudio.Count > 0)
                                                       {
                                                           lbxFavoriteaudio.ItemsSource = showFavAudio;

                                                           tblkFavNoAudio.Visibility = Visibility.Collapsed;
                                                       }
                                                       else
                                                       {
                                                           tblkFavNoAudio.Text = "No " + AppResources.FavoriteSongsPivotTitle + " available";
                                                           tblkFavNoAudio.Visibility = Visibility.Visible;
                                                       }
                                                   }
                                                 );
                bwHelper.RunBackgroundWorkers();
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in UserControl_Loaded Method In FavoriteAudio.cs file.", ex);
            }
        }

        private void lbxFavoriteaudio_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbxFavoriteaudio.SelectedIndex == -1)
                return;
            ShowAudio showaudio = new ShowAudio();

            string id = (lbxFavoriteaudio.SelectedItem as ShowLinkInfo).ID.ToString();
            string u = (lbxFavoriteaudio.SelectedItem as ShowLinkInfo).Songplay;
            string title = (lbxFavoriteaudio.SelectedItem as ShowLinkInfo).Title;
            string link = (lbxFavoriteaudio.SelectedItem as ShowLinkInfo).Link;
            showaudio.LoadDownLoads(id, u, title, link);
            AppSettings.SongID = (lbxFavoriteaudio.SelectedItem as ShowLinkInfo).ID.ToString();
            AppSettings.AudioImage = (lbxFavoriteaudio.SelectedItem as ShowLinkInfo).Songplay;
            Thread AudioThread = new Thread(() =>
            {
                showFavAudio = FavoritesManager.GetFavoriteAudioLinks();
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    lbxFavoriteaudio.ItemsSource = showFavAudio;
                });

            });
            AudioThread.Start();
            lbxFavoriteaudio.SelectedIndex = -1;
        }

        private void btnAudioFavSong_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OnlineVideos.View_Models.ShowDetail detailModel = new OnlineVideos.View_Models.ShowDetail();
                detailModel.AddToFavorites(lbxFavoriteaudio, sender as MenuItem, LinkType.Audio);
                PageHelper.NavigateTo(NavigationHelper.MainPanoramaPage);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in btnAudioFavSong_Click Method In FavoriteAudio.cs file.", ex);
            }
        }
    }
}
